<?php

App::uses('AppController', 'Controller');

class ProdavniceController extends AppController {
	
	public $uses = array('Store', 'Client');
	
	public $paginate = array(
		'limit' => ITEMS_PER_PAGE
	);
	
	public function _setCommonData() {
		$this->set('clients', $this->Client->find('list'));
		$this->set('contact_types', array(
			'izvršni direktor'	=> 'izvršni direktor',
			'poslovođa'			=> 'poslovođa',
			'RMO'				=> 'RMO'
		));
	}
	
	public function index() {
		$this->_setCommonData();
		
		$conditions = array();
		$filter = $this->params->query;
		if(!empty($filter)) {
			$conditions = array('AND' => array(1), 'OR'=>array(0));
			$filter['keyword'] = trim($filter['keyword']);
			$this->set('filter', $filter);
			
			if(!empty($filter['keyword'])) {
				$conditions['OR']['Store.name LIKE'] = '%' . $filter['keyword'] . '%';
				$conditions['OR']['Store.city LIKE'] = '%' . $filter['keyword'] . '%';
				$conditions['OR']['Store.township LIKE'] = '%' . $filter['keyword'] . '%';
				$conditions['OR']['Store.address LIKE'] = '%' . $filter['keyword'] . '%';
				$conditions['OR']['Store.phone LIKE'] = '%' . $filter['keyword'] . '%';
			}
			
			if(!empty($filter['client'])) {
				$conditions['AND']['Store.client_id'] = $filter['client'];
			}
			
			if(!empty($filter['status'])) {
				$conditions['AND']['Store.active'] = $filter['status'];
			}
		}
		
		$this->set("data", $this->paginate('Store', $conditions));
	}
	
	public function search($kword=null) {
		$this->autoRender = false;
		
		$stores = $this->Store->find('all', array(
			'fields' => array(
			    'Store.id',
			    'IF(Store.name = "", Client.name, Store.name) as StoreName',
			    'CONCAT_WS(", ", Store.address, Store.township, Store.city) as StoreLocation'
			),
			'conditions' => array(
				'OR' => array(
					'Store.name     LIKE' => "%$kword%",
					'Store.format   LIKE' => "%$kword%",
					'Store.address  LIKE' => "%$kword%",
					'Store.township LIKE' => "%$kword%",
					'Store.city     LIKE' => "%$kword%",
					'Client.name    LIKE' => "%$kword%"
				)
			),
			'order' => 'Store.city, Store.id'
		));

		$named = $this->params['named'];
		if($named['json']) {
			$labels = Set::format($stores, '{0} ({1})', array('{n}.0.StoreName', '{n}.0.StoreLocation'));
			
			$ids = Set::extract('{n}.Store.id', $stores);
			if(empty($ids)){
				$stores = array();
			}else{
				$stores = array_combine($ids, $labels);
			}
			echo json_encode($stores);
		}else{
			//TODO: render search result view
		}
	}
	
	public function nova() {
		$this->_setCommonData();
		$this->set("store", $this->Store->create());
		$this->render("form");
	}
	
	public function izmeni($id) {
		$this->_setCommonData();
		$this->set("store", $this->Store->findById($id));
		$this->render("form");
	}
	
	public function sacuvaj() {
		if(!empty($this->data)) {
			$data = $this->data;
			$this->set("data", $data);
			
			$this->Store->create($data);
			if($this->Store->validates()){
				if($this->Store->id>0) {
					$this->Store->Contact->deleteAll(array("store_id"=>$this->Store->id));
				}
				if($this->Store->saveAll($data)) {
					$this->setFlash("Prodavnica je uspešno sačuvana.", "ok");
					$this->redirect('izmeni/'.$this->Store->id);
				}else{
					$this->_setCommonData();
					$this->set("store", $data);
					$this->setFlash("Čuvanje nije uspelo.");
					return $this->render("form");
				}
			}else{
				$this->_setCommonData();
				$this->set("store", $data);
				$this->setFlash("Uneti podaci nisu validni. Čuvanje nije uspelo.");
				return $this->render("form");
			}
		}
		$this->redirect("index");
	}
	
	public function obrisi($id) {
		if($this->Store->delete($id)) {
			$this->setFlash("Prodavnica je obrisana.");
		}
		$this->redirect("index");
	}
}

?>